package org.dawn.deveco.plugin.resourceeditor.window.dialog;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import org.dawn.deveco.plugin.resourceeditor.common.Constants;
import org.dawn.deveco.plugin.resourceeditor.utils.Logger;
import org.dawn.deveco.plugin.resourceeditor.utils.SwingUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class MediaInputDialog extends DialogWrapper {
    private static final Logger LOG = Logger.getInstance(MediaInputDialog.class);

    private JLabel iconLabel;
    private JTextField iconPathField;
    private JButton selectIconButton;
    private JLabel imageLabel;

    private String selectedFilePath;

    public String getSelectedFilePath() {
        return selectedFilePath;
    }

    private final Action selectFileButtonAction = new AbstractAction() {
        @Override
        public void actionPerformed(@NotNull ActionEvent e) {
            FileChooserDescriptor chooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor()
                    .withFileFilter(file ->
                            Arrays.stream(Constants.IMAGE_FILE_EXTENSIONS).anyMatch(ext -> ext.equalsIgnoreCase(file.getExtension()))
                    );
            VirtualFile virtualFile = FileChooser.chooseFile(chooserDescriptor, null, null);
            if (virtualFile != null) {
                selectedFilePath = virtualFile.getPath();
            } else {
                Messages.showMessageDialog("文件名称为空", "文件名称为空", Messages.getInformationIcon());
            }
        }
    };

    public MediaInputDialog(@Nullable Project project) {
        super(project);
        setTitle("Input a Media");
        init();
    }

    //    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel dialogPanel = new JPanel(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setSize(Integer.MAX_VALUE, 50);

        iconLabel = new JLabel("资源文件");
        iconPathField = new JTextField(25);
        iconPathField.setPreferredSize(new Dimension(80, 20));
        iconPathField.setEditable(false);

        ImageIcon icon = SwingUtils.shrinkIcon(Constants.FILE_ICON_PATH, 16);
        selectIconButton = new JButton(icon);
        selectIconButton.setPreferredSize(new Dimension(20, 20));
        selectIconButton.addActionListener(this.selectFileButtonAction);

        inputPanel.add(iconLabel);
        inputPanel.add(iconPathField);
        inputPanel.add(selectIconButton);

        GridBagLayout layout = new GridBagLayout();
        JPanel imagePanel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        imageLabel = new JLabel("图像");
        // 设置GridBagConstraints属性，使按钮居中
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridwidth = GridBagConstraints.REMAINDER; // 使该组件占据所有可用的水平空间
        constraints.gridheight = GridBagConstraints.REMAINDER; // 使该组件占据所有可用的垂直空间
        constraints.weightx = 1.0; // 水平方向上的伸缩权重
        constraints.weighty = 1.0; // 垂直方向上的伸缩权重

        layout.setConstraints(imageLabel, constraints);
        imagePanel.add(imageLabel);

        dialogPanel.add(inputPanel, BorderLayout.NORTH);
        dialogPanel.add(imagePanel, BorderLayout.CENTER);

        return dialogPanel;
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            MediaInputDialog dialog = new MediaInputDialog(null);
//            dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//            JComponent centerPanel = dialog.createCenterPanel();
//            dialog.getContentPane().add(centerPanel);
//            dialog.setSize(400, 300);
////            dialog.pack();
//            dialog.show();
//        });
    }
}
